package com.tcm.doctor.web.controller;

import com.base.core.exception.ServiceException;
import com.base.core.pager.PageBean;
import com.base.core.utils.R;
import com.tcm.consultation.dto.ConsultationDTO;
import com.tcm.consultation.service.DoctorConsultationService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/doctor")
@RestController
public class ConsultationController {

    @DubboReference(version = "0.0.1-alpha", timeout = 6000, check = false, retries = 3)
    private DoctorConsultationService doctorConsultationService;

    @GetMapping("/consult/view")
    public R view(@RequestParam("id") Long id) {
        ConsultationDTO consultationDTO;
        try{
            consultationDTO = doctorConsultationService.findConsultationById(id);
        }catch (ServiceException e){
            return R.fail(e.getResult().getCode(), e.getMessage());
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
       return R.success(consultationDTO);
    }

    /**
     * 获取问诊单列表
     * @param doctorId
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/consult/list/page")
    public R listPage(@RequestParam Long doctorId,
                      @RequestParam(defaultValue = "1") Long page,
                      @RequestParam(defaultValue = "10") Long pagesize) {
        try{
            PageBean<ConsultationDTO> consultationDTOPageBean;
            consultationDTOPageBean = doctorConsultationService.listPageConsultation(doctorId,page,pagesize);
            return R.success(consultationDTOPageBean);
        }catch (ServiceException e){
            return R.fail(e.getResult().getCode(), e.getMessage());
        }catch (Exception e){
            return R.fail(e.getMessage());
        }

    }

    /**
     * 获取问诊单列表
     * @param doctorId
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/consult/list/page/status")
    public R listPageByStatus(@RequestParam Long doctorId,
                      @RequestParam Byte status,
                      @RequestParam(defaultValue = "1") Long page,
                      @RequestParam(defaultValue = "10") Long pagesize) {
        try{
            PageBean<ConsultationDTO> consultationDTOPageBean;
            consultationDTOPageBean = doctorConsultationService.listPageConsultation(doctorId,status, page,pagesize);
            return R.success(consultationDTOPageBean);
        }catch (ServiceException e){
            return R.fail(e.getResult().getCode(), e.getMessage());
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }

    /**
     * 统计订单数
     * @param doctorId
     * @param status
     * @return
     */
    @GetMapping("/consult/count/status")
    public R countByStatus(@RequestParam Long doctorId,
                              @RequestParam Byte status) {
        try{
            Long count = doctorConsultationService.countConsultation(doctorId,status);
            return R.success(count);
        }catch (ServiceException e){
            return R.fail(e.getResult().getCode(), e.getMessage());
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }

    /**
     * 统计全部订单数
     * @param doctorId
     * @return
     */
    @GetMapping("/consult/count")
    public R countConsultation(@RequestParam Long doctorId) {
        try{
            Long count = doctorConsultationService.countConsultation(doctorId,null);
            return R.success(count);
        }catch (ServiceException e){
            return R.fail(e.getResult().getCode(), e.getMessage());
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }

    /**
     * 接诊后，订单状态改为问诊中
     * @param id
     * @return
     */
    @GetMapping("/consult/change")
    public R change(@RequestParam Long id) {
        try{
            doctorConsultationService.changeStatus(id);
            return R.success();
        }catch (ServiceException e){
            return R.fail(e.getResult().getCode(), e.getMessage());
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }
}

